﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BaseSet.Data;
using BaseSet.Logic;
using BaseSet.Entity;
using BaseSet.Framework;
using BaseSet.Entity.Common;
using BaseSet.Entity.Common.Text;
using BaseSet;
using System.Data;


namespace WebSite.UserCash
{
    public partial class UserCashApply : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                LoadContent();
                BindCash();//加载取现金额
            }
        }

        #region 提交
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            int applymoney =Convert.ToInt32( ddlApplyMoney.SelectedValue.ToString());
            if (applymoney == 0)
            {
                Tools.Alert("取现金额不能为0！");
                return;
            }
            else
            {
                Users uinfo = UserBase.GetCurrentUser();
                UsersHelper uhelper = new UsersHelper();
                string userid = uinfo.ID;
                uinfo = uhelper.GetUsers(userid);
                CashApply cainfo = new CashApply();
                CashApplyHelper cahelper = new CashApplyHelper();
                cainfo.ApplyMoney = applymoney;
                cainfo.Status = 0;
                cainfo.UsersID = userid;
                string paytype = "";
                string paycontent = "";
                if (rbAliPay.Checked)
                {
                    //判断支付宝账号是否为空
                    string alipay = uinfo.AliPayAccountNumber;
                    if (string.IsNullOrEmpty(alipay))
                    {
                        Tools.Alert("支付宝账号为空，请添加！");
                        return;
                    }
                    paytype = "支付宝账号";
                    paycontent = litAliPayNumber.Text;
                }
                else if (rbBank.Checked)
                {
                    //判断银行账号是否为空
                    string bankaccountnumber = uinfo.BankAccountNumber;
                    string bankname = uinfo.BankName;
                    string openbankname = uinfo.OpenBankName;
                    string bankaccountname = uinfo.BankAccountName;
                    if (string.IsNullOrEmpty(bankaccountname) || string.IsNullOrEmpty(openbankname) || string.IsNullOrEmpty(bankname) || string.IsNullOrEmpty(bankaccountnumber))
                    {
                        Tools.Alert("银行账号添加不完整，请添加！");
                        return;
                    }

                    paytype = "银行账号";
                    paycontent = litBankAccountNumber.Text;
                }
                cainfo.PayType = paytype;
                cainfo.PayContent = paycontent;

                cahelper.AddCashApply(cainfo);

                //插入到现金流表中
                CashFlow cfinfo = new CashFlow();
                CashFlowHelper cfhelper = new CashFlowHelper();
                cfinfo.UsersID = uinfo.ID;
                cfinfo.Type = 5;
                cfinfo.Money = applymoney * -1;
                cfinfo.Description = "提现" + applymoney.ToString();
                cfhelper.AddCashFlow(cfinfo);

                Tools.AlertAndRedirect("申请提现成功，请等待管理员审批，审批通过后会将现金打到您选择的支付方式中。", "CashApplyList.aspx");
                return;
            }
        }
        #endregion

        #region 加载用户信息
        private void LoadContent()
        {
            Users uinfo = UserBase.GetCurrentUser();
            string userid = uinfo.ID;
            UsersHelper uhelper = new UsersHelper();
            uinfo = uhelper.GetUsers(userid);

            float totalmoney = uhelper.GetCashMoney(userid);
            lblMoney.Text = totalmoney.ToString();
            lblMoney1.Text = totalmoney.ToString();

            //加载当前用户的权限和最低取现金额
            string roleid = uinfo.RoleID;
            Role rinfo = new Role();
            RoleHelper rhelper = new RoleHelper();
            rinfo = rhelper.GetRole(roleid);
            string rolename = rinfo.RoleName;
            int cashmoney = rinfo.CashMoney;

            litRoleName.Text = rolename;
            litCashMoney.Text = cashmoney.ToString();

            //加载支付宝和银行账号信息
            string strbank = "";
            
            string alipaynumber = uinfo.AliPayAccountNumber;
            string bankaccountnumber = uinfo.BankAccountNumber;

            string bankname = uinfo.BankName;
            string openbankname = uinfo.OpenBankName;
            string bankaccountname = uinfo.BankAccountName;

            if (alipaynumber == "")
            {
                alipaynumber = "为空";
            }
            if (bankaccountnumber == "")
            {
                strbank = "为空";
            }
            else
            {
                strbank = bankaccountnumber + "-" + bankaccountname + "-" + openbankname + "-" + bankname;
            }
            litAliPayNumber.Text = alipaynumber;
            litBankAccountNumber.Text = strbank;

            //if (totalmoney >= cashmoney)
            //{
            //    btnSubmit.Enabled = true;
            //}
            //else
            //{
            //    btnSubmit.Enabled = false;
            //}
        }
        #endregion

        #region 加载取现金额
        private void BindCash()
        {
            int minmoney = Convert.ToInt32(litCashMoney.Text);//最低提现金额
            float money = (float)Convert.ToDouble(lblMoney.Text);//当前可提现余额

            if (money >= minmoney)
            {
                //ListItem li = new ListItem();
                //li.Text = minmoney.ToString();
                //li.Value = minmoney.ToString();
                //ddlApplyMoney.Items.Insert(0, li);

                int i = 0;
                while (minmoney+100 * i < money)
                {
                    ListItem li1 = new ListItem();
                    li1.Text = (minmoney + 100 * i).ToString();
                    li1.Value = (minmoney + 100 * i).ToString();
                    ddlApplyMoney.Items.Insert(i, li1);
                    i++;
                }
            }
            else//0
            {
                ListItem li = new ListItem();
                li.Text = "0";
                li.Value = "0";
                ddlApplyMoney.Items.Insert(0, li);
            }
        }
        #endregion
    }
}